package com.efast.dmom.mgmt.controller.les;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.dmom.mgmt.bean.les.GoodsReceiptBean;
import com.efast.dmom.mgmt.entity.les.TwDocAsnHeader;
import com.efast.dmom.mgmt.service.les.TwDocAsnHeaderService;
import com.efast.dmom.mgmt.utils.Assist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author chlp
 */
@Controller
@RequestMapping(value = "/twDocAsnHeader")
@Api(description = "ASN Header")
public class TwDocAsnHeaderController {

	private final TwDocAsnHeaderService twDocAsnHeaderService;

	@Autowired
	public TwDocAsnHeaderController(TwDocAsnHeaderService twDocAsnHeaderService) {
		this.twDocAsnHeaderService = twDocAsnHeaderService;
	}

	// ASN HEADER单据创建[生成ASN序列号，ASN状态改为0(即新单)]
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultBean save(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		twDocAsnHeaderService.insertTwDocAsnHeader(twDocAsnHeader);
		return result;
	}

	// ASN HEADER的关闭[ASN状态改为5(即单证关闭)]
	@RequestMapping(value = "/headerClose", method = RequestMethod.POST)
	public @ResponseBody ResultBean headerClose(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		twDocAsnHeader.setAsnSheetStatus("5");
		twDocAsnHeaderService.updateNonEmptyTwDocAsnHeaderById(twDocAsnHeader);
		return result;
	}

	/**
	 * 【确认收货】 HEADER的接收:ASN HEADER的接收[ASN状态改为2(即收货完成),收货完成后在TW_INBOUND_ALLOC_DETAIL创建数据]
	 * 
	 * @param goodsreceiptbean
	 *            { "listTwDocAsnDetails": [ { "asnLineno": 0, "asnPackageNo": "PACKAGE2222", "asnPackageNum": 0,
	 *            "asnPackageQty": 10, "asnReceivedQty": 2, "asnReqQty": 2 } ], "twDocAsnHeader": { "twDocAsnHeaderId":
	 *            0, "dockId": 1, "dockNo": "002", "twDocAsnHeaderId": 9 } }
	 */
	@RequestMapping(value = "/headerReceive", method = RequestMethod.POST)
	@ApiOperation(value = "【确认收货】", notes = "请求参数：")
	public @ResponseBody ResultBean headerReceive(@RequestBody GoodsReceiptBean goodsreceiptbean) {
		ResultBean result = new ResultBean(true, "'");
		twDocAsnHeaderService.headerReceive(goodsreceiptbean);
		return result;
	}

	/**
	 * HEADER的的拒绝:[ASN状态改为3(即收货拒绝)]
	 * 
	 * @param twDocAsnHeader
	 *            twDocAsnHeader
	 */
	@RequestMapping(value = "/refuseReceive", method = RequestMethod.POST)
	public @ResponseBody ResultBean refuseReceive(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		// ASN单证状态（新单0、部分收货1、收货完成2、拒绝收货3、收货取消4、单证关闭5）
		Assist assist = new Assist();
		twDocAsnHeader.setAsnSheetStatus("3");
		twDocAsnHeaderService.refuseReceive(twDocAsnHeader, assist);
		return result;
	}

	/**
	 * 取消收货操作[ASN状态改为4(收货取消)-只有部分收货/收货完成才能取消] （注：同时更新相关detail的相关的行的记录状态）
	 * 
	 * @param twDocAsnHeader
	 *            twDocAsnHeader
	 */
	@RequestMapping(value = "/cancelReceive", method = RequestMethod.POST)
	public @ResponseBody ResultBean cancelReceive(@RequestBody TwDocAsnHeader twDocAsnHeader) {
		ResultBean result = new ResultBean(true, "'");
		twDocAsnHeaderService.cancelTheReceiptOfTheGoods(twDocAsnHeader);
		return result;
	}

}
